package com.bot.edu.applysystem.applystuconsult.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bot.edu.utils.SnowFlake;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.bot.edu.applysystem.applystuconsult.entity.ApplyStuConsult;
import com.bot.edu.applysystem.applystuconsult.service.IApplyStuConsultService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 用户咨询管理
 * @Author: jeecg-boot
 * @Date:   2022-04-02
 * @Version: V1.0
 */
@Api(tags="用户咨询管理")
@RestController
@RequestMapping("/applystuconsult/applyStuConsult")
@Slf4j
@CrossOrigin
public class ApplyStuConsultController extends JeecgController<ApplyStuConsult, IApplyStuConsultService> {
	@Autowired
	private IApplyStuConsultService applyStuConsultService;
	
	/**
	 * 分页列表查询
	 *
	 * @param applyStuConsult
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "用户咨询管理-分页列表查询")
	@ApiOperation(value="用户咨询管理-分页列表查询", notes="用户咨询管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ApplyStuConsult>> queryPageList(ApplyStuConsult applyStuConsult,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ApplyStuConsult> queryWrapper = QueryGenerator.initQueryWrapper(applyStuConsult, req.getParameterMap());
		Page<ApplyStuConsult> page = new Page<ApplyStuConsult>(pageNo, pageSize);
		IPage<ApplyStuConsult> pageList = applyStuConsultService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param applyStuConsult
	 * @return
	 */
	@AutoLog(value = "用户咨询管理-添加")
	@ApiOperation(value="用户咨询管理-添加", notes="用户咨询管理-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ApplyStuConsult applyStuConsult) {
        String id = SnowFlake.getId();
        applyStuConsult.setConsultId(id);
        applyStuConsultService.save(applyStuConsult);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param applyStuConsult
	 * @return
	 */
	@AutoLog(value = "用户咨询管理-编辑")
	@ApiOperation(value="用户咨询管理-编辑", notes="用户咨询管理-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ApplyStuConsult applyStuConsult) {
		applyStuConsultService.updateById(applyStuConsult);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "用户咨询管理-通过id删除")
	@ApiOperation(value="用户咨询管理-通过id删除", notes="用户咨询管理-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		applyStuConsultService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "用户咨询管理-批量删除")
	@ApiOperation(value="用户咨询管理-批量删除", notes="用户咨询管理-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.applyStuConsultService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "用户咨询管理-通过id查询")
	@ApiOperation(value="用户咨询管理-通过id查询", notes="用户咨询管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ApplyStuConsult> queryById(@RequestParam(name="id",required=true) String id) {
		ApplyStuConsult applyStuConsult = applyStuConsultService.getById(id);
		if(applyStuConsult==null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(applyStuConsult);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param applyStuConsult
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ApplyStuConsult applyStuConsult) {
        return super.exportXls(request, applyStuConsult, ApplyStuConsult.class, "用户咨询管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ApplyStuConsult.class);
    }

     /**
      * 咨询用户查询
      * @return
      */
    @RequestMapping(value = "/applyStuConsultList",method = RequestMethod.GET)
     public Result<?> applyStuConsultList(){
        List<ApplyStuConsult> applyStuConsultList = applyStuConsultService.list();
        return Result.ok(applyStuConsultList);
    }

     /**
      * 根据手机号查询咨询记录
      * @param consultPhone
      * @return
      */
    @GetMapping("/queryByPhone")
    public Result<?> StuConsultBuPhone(String consultPhone){
        QueryWrapper<ApplyStuConsult> applyStuConsultQueryWrapper = new QueryWrapper<>();
        applyStuConsultQueryWrapper.lambda().eq(ApplyStuConsult::getConsultPhone,consultPhone);
        ApplyStuConsult stuConsult = applyStuConsultService.getOne(applyStuConsultQueryWrapper);
        return Result.ok(stuConsult);
    }


     /**
      * 增加咨询记录
      * @param applyStuConsult
      * @return
      */
    @PostMapping("/addConsult")
     public Result<?> addConsult(@RequestBody ApplyStuConsult applyStuConsult){
        applyStuConsult.setId(SnowFlake.getId());
        applyStuConsult.setConsultId(SnowFlake.getId());
        applyStuConsult.setDelFlag(0);
        applyStuConsult.setConsultTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"");
        applyStuConsultService.save(applyStuConsult);
        return Result.ok();
    }

     /**
      * 修改咨询
      * @param applyStuConsult
      * @return
      */
    @PostMapping("/upConsult")
     public Result<?> upConsult(@RequestBody ApplyStuConsult applyStuConsult){
        applyStuConsult.setConsultTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"");
        applyStuConsultService.updateById(applyStuConsult);
        return Result.ok();
    }

}
